package com.alan.wifishare.view.impl.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.presenter.CNTLinkManagerPS;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.iview.IVCNTLinkManager;
import com.alan.wifishare.view.ui.TwoBtnDialog;

/**
 * @className: CNTLinkManagerActivity
 * @describtion: CNTLinkManagerActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 13:48
 */
public class CNTLinkManagerActivity extends BaseActivity implements IVCNTLinkManager, View.OnClickListener{

    private CNTLinkManagerPS cntLinkManagerPS = null;
    private Button cntLinkManageBackBtn;
    private TextView cntLinkManageTitleTv;
    private RelativeLayout cntLinkManageBindLinkRl;
    private RelativeLayout cntLinkManageUnBindLinkRl;
    private RelativeLayout cntLinkManageChangeLinkRl;
    private RelativeLayout cntLinkManageManageDeviceRl;
    private TwoBtnDialog twoBtnDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cnt_link_manage_layout);
        cntLinkManagerPS = new CNTLinkManagerPS(this);
        initView();
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        String linkName = bundle.getString(ActivityJumpUtil.Extra.LINK_NAME);
        cntLinkManageTitleTv = (TextView)findViewById(R.id.cntLinkManageTitleTv);
        cntLinkManageTitleTv.setText(linkName);
        cntLinkManageBackBtn = (Button)findViewById(R.id.cntLinkManageBackBtn);
        cntLinkManageBindLinkRl = (RelativeLayout)findViewById(R.id.cntLinkManageBindLinkRl);
        cntLinkManageUnBindLinkRl = (RelativeLayout)findViewById(R.id.cntLinkManageUnBindLinkRl);
        cntLinkManageChangeLinkRl = (RelativeLayout)findViewById(R.id.cntLinkManageChangeLinkRl);
        cntLinkManageManageDeviceRl = (RelativeLayout)findViewById(R.id.cntLinkManageManageDeviceRl);

        cntLinkManageBackBtn.setOnClickListener(this);
        cntLinkManageBindLinkRl.setOnClickListener(this);
        cntLinkManageUnBindLinkRl.setOnClickListener(this);
        cntLinkManageChangeLinkRl.setOnClickListener(this);
        cntLinkManageManageDeviceRl.setOnClickListener(this);
    }

    @Override
    public void unBoundLinkSuccess() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(twoBtnDialog != null & twoBtnDialog.isShowing()){
            twoBtnDialog.dismiss();
        }
    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cntLinkManageBackBtn:
                CNTLinkManagerActivity.this.finish();
                break;
            case R.id.cntLinkManageBindLinkRl:
                if(GlobalField.userBoundDevice == null){
                    ActivityJumpUtil.jump2BindLinkScanAct(CNTLinkManagerActivity.this);
                }else {
                    toastMsg("您已绑定有设备，如要重新绑定请先解绑");
                }
                break;
            case R.id.cntLinkManageUnBindLinkRl:
                initTwoBtnDialog();
                twoBtnDialog.show();
                break;
            case R.id.cntLinkManageChangeLinkRl:
                ActivityJumpUtil.jump2ChangeLinkAct(CNTLinkManagerActivity.this);
                break;
            case R.id.cntLinkManageManageDeviceRl:
                ActivityJumpUtil.jump2ManageConnectedDeviceAct(CNTLinkManagerActivity.this);
                break;
            default:
                break;
        }
    }

    private void initTwoBtnDialog(){
        if(twoBtnDialog == null){
            twoBtnDialog = new TwoBtnDialog(this, R.style.custom_dialog1);
            twoBtnDialog.setTitle("解绑提示");
            twoBtnDialog.setMessage("解绑后将无法上网，是否确认解绑？");
            twoBtnDialog.setLeftBtn("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    twoBtnDialog.dismiss();
                }
            });
            twoBtnDialog.setRightBtn("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    cntLinkManagerPS.unbindDevice();
                    twoBtnDialog.dismiss();
                }
            });
        }
    }
}
